﻿using System;
using System.Linq;
using System.Web.UI.WebControls;
using TravelAgency.DAL;

namespace TravelAgency.AspNet.Agents
{
    public partial class EditTrip : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //string tripIdString = this.Request.QueryString["tripId"];
            //int tripId = int.Parse(tripIdString);
            //TravelAgencyDAO dao = new TravelAgencyDAO();
            //Trip trip;
            //using (dao)
            //{
            //    trip = dao.GetTripByTripId(tripId);
            //    this.FillTripDate(trip);
            //}
            //this.RangeValidatorPeopleCount.MinimumValue = trip.CurrentPeopleCapacity.ToString();
        }

        protected void Page_PreRender(object sender, EventArgs e)
        {
            string tripIdString = this.Request.QueryString["tripId"];
            int tripId = int.Parse(tripIdString);
            TravelAgencyDAO dao = new TravelAgencyDAO();
            using (dao)
            {
                Trip trip = dao.GetTripByTripId(tripId);
                this.FillTripDate(trip);
                this.RangeValidatorPeopleCount.MinimumValue = trip.CurrentPeopleCapacity.ToString();
            }
        }


        private void FillTripDate(Trip selectedTrip)
        {
            if (selectedTrip != null)
            {
                int tripCurrentCalacity = selectedTrip.CurrentPeopleCapacity;
                this.PanelTripDetails.Visible = true;
                this.TextBoxTitle.Text = selectedTrip.Title;
                this.TextBoxDescription.Text = selectedTrip.Description;
                this.TextBoxPrice.Text = selectedTrip.Price.ToString();
                this.RadDatePickerStartDate.SelectedDate = selectedTrip.StartDate;
                this.RadDatePickerEndDate.SelectedDate = selectedTrip.EndDate;
                this.TextBoxPeopleCount.Text = tripCurrentCalacity.ToString();
                this.TextBoxMaxPeopleCount.Text = selectedTrip.MaxPeopleCapacity.ToString();
            }
        }

        protected void LinkButtonCancelReservation_Click(object sender, EventArgs e)
        {
            this.Response.Redirect("~/Agents/ManageTrips.aspx");
        }

        protected void LinkButtonUpdate_Click(object sender, EventArgs e)
        {
            Page.Validate();

            if (Page.IsValid)
            {
                string tripIdString = this.Request.QueryString["tripId"];
                int tripId = int.Parse(tripIdString);
                string title = this.TextBoxTitle.Text;
                string description = this.TextBoxDescription.Text;
                int capacity = int.Parse(this.TextBoxMaxPeopleCount.Text);
                decimal price = (decimal)double.Parse(this.TextBoxPrice.Text);
                DateTime startDate = (DateTime)this.RadDatePickerStartDate.SelectedDate;
                DateTime endDate = (DateTime)this.RadDatePickerEndDate.SelectedDate;

                TravelAgencyDAO dao = new TravelAgencyDAO();
                using (dao)
                {
                    try
                    {
                        dao.UpdateTrip(tripId, title, description, startDate, endDate, price, capacity);
                        dao.SaveChanges();
                        this.Response.Redirect("~/Agents/TripEdited.aspx");
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }
                }
            }
        }        

        protected void CustomValidatorStartDate_ServerValidate(object source, System.Web.UI.WebControls.ServerValidateEventArgs args)
        {
            ValidateDate(args);
        }
  
        
        protected void CustomValidatorEndDate_ServerValidate(object source, System.Web.UI.WebControls.ServerValidateEventArgs args)
        {
            ValidateDate(args);
        }

        private void ValidateDate(ServerValidateEventArgs args)
        {
            if (this.RadDatePickerStartDate.SelectedDate == null ||
                this.RadDatePickerEndDate.SelectedDate == null)
            {
                args.IsValid = false;
            }

            bool isControlValid =
            this.RadDatePickerStartDate.SelectedDate <= this.RadDatePickerEndDate.SelectedDate;

            args.IsValid = isControlValid;
        }
    }
}